/*
 * @lc app=leetcode.cn id=395 lang=golang
 *
 * [395] 至少有 K 个重复字符的最长子串
 */
package Solutions

import "strings"

// @lc code=start
func longestSubstring(s string, k int) int {
	if s == "" {
		return 0
	}
	var cnt = [26]int{}
	for _, c := range s {
		cnt[c-'a']++
	}
	var sub byte
	for i, v := range cnt {
		if v > 0 && v < k {
			sub = 'a' + byte(i)
			break
		}
	}
	if sub == 0 {
		return len(s)
	}
	var ans = 0
	for _, v := range strings.Split(s, string(sub)) {
		ans = max_longe(ans, longestSubstring(v, k))
	}
	return ans
}
func max_longe(i, j int) int {
	if i < j {
		return j
	}
	return i
}

// @lc code=end
